<?php

if (!defined('_CAN_LOAD_FILES_'))
    exit;

class Blockopinion extends Module {

    public function __construct() {
        $this->name = 'blockopinion';
        $this->tab = 'Opinie';
        $this->tabClassName = 'AdminOpinion';
        $this->tabParentName = 'AdminCustomers';
        $this->need_instance = 0;
        parent::__construct();
        $this->displayName = $this->l('Blok opinii o sklepie soczewki.net');
        $this->description = $this->l('Mechanizm wyświetla zatwierdzone przez admina opinie');
        $this->confirmUninstall = $this->l('Are you sure you want to delete all your opinions?');
        $this->version = '1.0';
        $this->author = 'CreativeCoder.pl';
        $this->error = false;
        $this->valid = false;
    }

    public function install() {
        if (!parent::install() ||
                !$this->registerHook('leftColumn') ||
                !$this->registerHook('header') ||
                !$this->registerHook('footer') ||
                !$this->installModuleTab())
            return false;
        return Db::getInstance()->Execute('
		CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'opinion (
			`id_opinion` int(6) NOT NULL AUTO_INCREMENT,
			`name` varchar(255) NOT NULL,
                        `rating` int NOT NULL,
                        `message` text NOT NULL,
                        `visible` boolean DEFAULT false,
			`created_at` DATETIME NULL,
			PRIMARY KEY(`id_opinion`)
		) ENGINE=' . _MYSQL_ENGINE_ . ' default CHARSET=utf8');
    }

    public function uninstall() {
        if (!parent::uninstall() ||
                !$this->uninstallModuleTab('AdminOpinion'))
            return false;
        return Db::getInstance()->Execute('DROP TABLE ' . _DB_PREFIX_ . 'opinion');
    }

    function hookLeftColumn($params) {
        global $smarty;
        return $this->display(__FILE__, 'blockopinion.tpl');
    }

    public function confirmation() {
//		global $smarty;
//		return $this->display(__FILE__, 'newsletter.tpl');
    }

    function hookHeader($params) {
        //Tools::addCSS(($this->_path).'blocknewsletter.css', 'all');
    }

    function hookFooter($params) {
        global $smarty;
        $opinions = Opinion::fetchAllVisible();
        $smarty->assign('opinions', $opinions);
        return $this->display(__FILE__, 'blockopinion-footer.tpl');
    }

    private function installModuleTab() {
        @copy(_PS_MODULE_DIR_ . $this->name . '/logo.gif', _PS_IMG_DIR_ . 't/' . $tabClass . '.gif');
        $tab = new Tab();
        $tab->class_name = $this->tabClassName;
        $tab->module = $this->name;
        $tab->id_parent = Tab::getIdFromClassName($this->tabParentName);
        $languages = Language::getLanguages();
        foreach ($languages as $language)
            $tab->name[$language['id_lang']] = $this->tab;
        $tab->add();
        return true;
    }

    private function uninstallModuleTab() {
        $idTab = Tab::getIdFromClassName($this->tabClassName);
        if ($idTab != 0) {
            $tab = new Tab($idTab);
            $tab->delete();
            return true;
        }
        return false;
    }
    

}

